package com.microframework.base.core.util.sql;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

public class TableMetaUtil {
    
    /**
     * 获取表的字段信息
     * @param tableName 表名
     * @return 字段信息列表
     */
    public static List<Record> getTableColumns(String tableName) {
        String sql = "SELECT COLUMN_NAME AS field, DATA_TYPE AS type, " +
                     "COLUMN_COMMENT AS comment, IS_NULLABLE AS nullable " +
                     "FROM information_schema.COLUMNS " +
                     "WHERE TABLE_SCHEMA = DATABASE() " +  // 当前数据库
                     "AND TABLE_NAME = ? " +
                     "ORDER BY ORDINAL_POSITION";
        
        return Db.find(sql, tableName);
    }

    public static void main(String[] args) {
        List<Record> columns = getTableColumns("your_table_name");
        for (Record col : columns) {
            System.out.println("字段: " + col.getStr("field"));
            System.out.println("类型: " + col.getStr("type"));
            System.out.println("注释: " + col.getStr("comment"));
            System.out.println("允许空: " + col.getStr("nullable"));
            System.out.println("---------------");
        }
    }
}